package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.CallDeviceWatchDTO;
import com.hksj.Intelligentmanagement.entity.CallDeviceWatchEntity;
import com.hksj.Intelligentmanagement.mapper.CallDeviceWatchMapper;
import com.hksj.Intelligentmanagement.vo.CallDeviceWatchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CallDeviceWatchService {

    @Autowired
    private CallDeviceWatchMapper callDeviceWatchMapper;

    //新增手环信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)//异常处理
    public Result createCallDeviceWatch(CallDeviceWatchDTO deviceWatchDTO) {
        QueryWrapper<CallDeviceWatchEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code", deviceWatchDTO.getCode());
        Integer count = callDeviceWatchMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error("手环编码已存在");
        }
        QueryWrapper<CallDeviceWatchEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("name", deviceWatchDTO.getName());
        Integer count1 = callDeviceWatchMapper.selectCount(wrapper1);
        if (count1 > 0) {
            return Result.error("手环名称已存在");
        }
        CallDeviceWatchEntity braceletEntity = new CallDeviceWatchEntity();
        BeanUtils.copyProperties(deviceWatchDTO, braceletEntity);
        //在表中创建一条记录
        int insert = callDeviceWatchMapper.insert(braceletEntity);
        if (insert <= 0) {
            return Result.error("新增失败");
        }
        return Result.ok("新增成功");
    }

    //删除手环信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteCallDeviceWatch(String id) {
        int insert = callDeviceWatchMapper.deleteById(id);
        if (insert <= 0) {
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }

    //更改手环信息
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateCallDeviceWatch(CallDeviceWatchDTO deviceWatchDTO) {
        CallDeviceWatchEntity braceletEntity = new CallDeviceWatchEntity();
        BeanUtils.copyProperties(deviceWatchDTO, braceletEntity);
        int i = callDeviceWatchMapper.updateById(braceletEntity);
        if (i <= 0) {
            return Result.error("更新失败");
        }
        return Result.ok("更新成功");
    }

    //根据id查询手环信息
    public Result queryCallDeviceWatchById(String id) {
        //调用selectById查询专业信息
        CallDeviceWatchEntity entity = callDeviceWatchMapper.selectById(id);
        //返回查询结果
        return Result.data(entity);
    }

    //模糊查询手环信息
    public Page<CallDeviceWatchVo> queryCallDeviceWatchListByPage(CallDeviceWatchDTO deviceWatchDTO) {
        //创建一个分页实例 mybatis plus的Page类型为IPage子类 能实现 对查询结果集排序 也能直接返回
        Page<CallDeviceWatchVo> page = new Page<>(deviceWatchDTO.getPageNum(), deviceWatchDTO.getPageSize());
        //在SQL查询结果集使用创建时间正序排序
        page.addOrder(OrderItem.asc("create_time"));
        //对查询进行分页
        List<CallDeviceWatchVo> braceletVos = callDeviceWatchMapper.queryBraceletListByPage(page, deviceWatchDTO);
        //保存分页记录
        page.setRecords(braceletVos);
        //返回分页
        return page;
    }
}
